package cn.com.youyuan.ui;

import cn.com.youyuan.display.GraphicsProxy;
import cn.com.youyuan.models.UIContainer;
import cn.com.youyuan.models.UIElement;

public class Table extends List
{
	private int m_backgroundColor = 0xffffff;
	private int m_backgroundColorOnFocus = 0xffffff;
	private int m_borderColor = 0xffffff;
	private int m_borderColorOnFocus = 0xffffff;
	private boolean isFullOfScreen;

	public boolean isFullOfScreen()
	{
		return isFullOfScreen;
	}

	public void setFullOfscreen(boolean isFullOf)
	{
		this.isFullOfScreen = isFullOf;
	}

	public Table(UIContainer parent, int left, int top, int width, int height)
	{
		super(parent, left, top, width, height, false);
	}

	protected int getMinHeight()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean canReceiveFocus()
	{
		for (int i = 0, j = getChildrenCount(); i < j; i++)
		{
			if (getChildAt(i).canReceiveFocus())
			{
				return true;
			}
		}
		return false;
	}

	public void onChildHeightChange(UIElement childChangedHeight, int difference)
	{
		int index = getChildIndex(childChangedHeight);

		if (index >= 0)
		{
			UIElement uie = null;
			int totalHeight = 0;
			for (int i = 0, j = getChildrenCount(); i < j; i++)
			{
				uie = getChildAt(i);
				totalHeight += uie.getHeight();
				if (i > index && childChangedHeight.getTop() != uie.getTop())
				{

					uie.setTop(uie.getTop() + difference);
				}
			}

			setHeight(uie.getHeight() + uie.getTop() + 1);
			getParent().onChildHeightChange(this, difference);
			((BasePage) getParent()).repaint();
		}
	}

	public void addChild(UIElement uie)
	{
		if (uie instanceof Tr)
		{
			uie.setLeft(getLeft());
			if (getFocusedItem() == null && uie.canReceiveFocus())
			{
				setFocusedItem(uie);
			}
			super.addChild(uie);
			setHeight(uie.getTop() + uie.getHeight() + 1);
		}

	}

	public void setLeft(int left)
	{
		// System.out.println("Table set : L : " + left);
		super.setLeft(left);
	}

	public boolean paint(GraphicsProxy g, int offsetHeight, boolean isFocused)
	{
		// fill(g, getLeft(), getTop() + offsetHeight, getWidth(), getHeight(),
		// getBackgroundColor());
		// drawBorder(g, getLeft(), getTop() + offsetHeight, getWidth(),
		// getHeight(), getBorderColor());

		return super.paint(g, offsetHeight, isFocused);
	}

	public int getBackgroundColor()
	{
		return m_backgroundColor;
	}

	public void setBackgroundColor(int backgroundColor)
	{
		m_backgroundColor = backgroundColor;
	}

	public int getBackgroundColorOnFocus()
	{
		return m_backgroundColorOnFocus;
	}

	public void setBackgroundColorOnFocus(int backgroundColorOnFocus)
	{
		m_backgroundColorOnFocus = backgroundColorOnFocus;
	}

	public int getBorderColor()
	{
		return m_borderColor;
	}

	public void setBorderColor(int borderColor)
	{
		m_borderColor = borderColor;
	}

	public int getBorderColorOnFocus()
	{
		return m_borderColorOnFocus;
	}

	public void setBorderColorOnFocus(int borderColorOnFocus)
	{
		m_borderColorOnFocus = borderColorOnFocus;
	}

	public void setTop(int top)
	{
		if (top == 60)
		{
			top = top;
		}
		System.out.println("Table set : T : " + top);
		super.setTop(top);
	}

	public void updateFocus(UIElement uie)
	{
		if (getFocusedItem() == null)
		{
			setFocusedItem(uie);
			((BasePage) getParent()).updateFocus(this);
		}
	}

	public int onPointerPress(int x, int y)
	{
		return super.onPointerPress(x, y);
	}
}
